import time
import random
from concurrent.futures import ThreadPoolExecutor, as_completed

import requests

# 测试路由
base_url = "http://127.0.0.1:8000/testRocks"
req_payload = {
    "id": 123,
    "change": 1,
}

req_headers = {}

executor = ThreadPoolExecutor(10)


def request_url(url: str, headers: dict, payload: dict) -> str:
    # time.sleep(random.randint(0,1))
    response = requests.request("POST", url, headers=headers, data=payload)
    # print("response>>> ", response.text)
    return response.text


if __name__ == '__main__':

    # 单独测试
    # request_url(base_url, req_headers, req_payload)

    # 并发测试
    # 线程池中并发执行任务
    all_tasks = [executor.submit(request_url, base_url, req_headers, req_payload) for i in range(100)]

    # 获取子线程中的结果 写入到文件中
    # with open("./req4.txt", "a+", encoding='utf-8')as f:
    #     for future in as_completed(all_tasks):
    #         text = future.result()
    #         print("rep_text:>>> ", text)
    #         f.write(text + "\n")
